﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common.Utility;
using FluentData;
using Tools.Model;

namespace WindowsTools.SqlOption
{
    public partial class FormSqlSrc : Form
    {
        private List<string> listDataBase = new List<string>();
        private List<Proc> listProcs = new List<Proc>();


        public FormSqlSrc()
        {
            InitializeComponent();
            BindData();
        }

        public void BindData()
        {
            BindChkListDatabaseName();
            BindChkListDatabaseName();
        }

        private void BindChkListDatabaseName()
        {
            chklistDatabaseName.Items.Add("PMS_Dev");
        }

        private void chklistDatabaseName_SelectedIndexChanged(object sender, EventArgs e)
        {
            listDataBase.Clear();
            listProcs.Clear();

            for (int i = 0; i < chklistDatabaseName.Items.Count; i++)
            {
                if (chklistDatabaseName.GetItemChecked(i))
                {
                    listDataBase.Add(chklistDatabaseName.GetItemText(chklistDatabaseName.Items[i]));
                }
            }

            if (listDataBase != null && listDataBase.Count > 0)
            {
                BindProcName(listDataBase);
            }

        }

        private void BindProcName(IEnumerable<string> listDataBaseName)
        {
            foreach (var strDataBaseName in listDataBaseName)
            {
                QueryProcList(strDataBaseName);
            }

            if (listProcs != null && listProcs.Count > 0)
            {
                foreach (Proc proc in listProcs)
                {
                    chklistProc.Items.Add(proc.name);
                }
            }
        }


        private void QueryProcList(string dataBaseName)
        {

            DBContext db = new DBContext();
            List<Proc> product = db.Context.Sql(
                string.Format(@"select '{0}' databasename,a.name,a.[type],b.[definition] from {0}.sys.all_objects a,{0}.sys.sql_modules b 
                                            where a.is_ms_shipped=0 and a.object_id = b.object_id and a.[type] in ('P','V','AF') 
                                            order by a.[name] ASC
                                            ", dataBaseName)).QueryMany<Proc>();

            //var store = db.Context.StoredProcedure("PF_Sys_PageControl")
            //    .ParameterOut("totalPage", DataTypes.Int16);

            //var result = store.QueryMany<Proc>();

            listProcs.AddRange(product);


        }

        private void Create_Proc_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < chklistProc.Items.Count; i++)
            {
                if (chklistProc.GetItemChecked(i))
                {
                    var procName = chklistProc.GetItemText(chklistProc.Items[i]);

                    ShowSqlFrom(procName);
                }
            }
        }


        private void ShowSqlFrom(string procName)
        {
            if (!string.IsNullOrWhiteSpace(procName))
            {

                var proc = listProcs.Where(p => p.name == procName).SingleOrDefault();

                if (proc != null)
                {
                    FormSQLShow fromSqlShow = new FormSQLShow();
                    fromSqlShow.sqlStr = proc.GetProcDefintion();
                    fromSqlShow.Bind();
                    fromSqlShow.Show();

                }

            }


        }
    }
}
